package org.example.school.controller;

import org.example.school.business.ExamInfo;
import org.example.school.common.Result;
import org.example.school.entity.Classes;
import org.example.school.entity.Exams;
import org.example.school.entity.Scores;
import org.example.school.model.dto.ExamScoreDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher/exam")
@CrossOrigin
public class ExamController {

    @Autowired
    private ExamInfo examInfo;

    /**
     * 发布考试
     * @param exam 考试信息
     * @return 发布结果
     */
    @PostMapping("/publish")
    public Result<String> publishExam(@RequestBody Exams exam) {
        return examInfo.publishExam(exam);
    }

    /**
     * 获取教师可以发布考试的班级列表
     * @return 班级列表
     */
    @GetMapping("/classes")
    public Result<List<Classes>> getTeachingClasses() {
        return examInfo.getTeachingClasses();
    }

    /**
     * 获取考试列表
     * @param classId 班级ID
     * @return 考试列表
     */
    @GetMapping("/list/{classId}")
    public Result<List<Exams>> getExamList(@PathVariable Integer classId) {
        return examInfo.getExamList(classId);
    }

    /**
     * 录入学生考试成绩
     * @param examId 考试ID
     * @param scores 成绩列表
     * @return 录入结果
     */
    @PostMapping("/scores/{examId}")
    public Result<String> recordScores(@PathVariable Long examId, @RequestBody List<Scores> scores) {
        return examInfo.recordScores(examId, scores);
    }

    /**
     * 获取考试成绩列表
     * @param examId 考试ID
     * @return 成绩列表
     */
    @GetMapping("/scores/{examId}")
    public Result<List<ExamScoreDTO>> getScoresByExamId(@PathVariable Long examId) {
        return examInfo.getScoresByExamId(examId);
    }

    /**
     * 更新学生成绩
     * @param scoreId 成绩ID
     * @param newScore 新分数
     * @return 更新结果
     */
    @PutMapping("/scores/{scoreId}")
    public Result<String> updateScore(@PathVariable Long scoreId, @RequestParam String newScore) {
        return examInfo.updateScore(scoreId, newScore);
    }
}